package com.suishoupai.xmb.tag;

import org.springframework.util.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by caojian on 2017/4/9.
 */
public class DateTag extends TagSupport {

    private static final long serialVersionUID = 6464168398214506236L;

    // 默认日期格式
    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm";

    private String value; // long型 时间戳
    private String pattern; // 日期格式，如果不传则为默认格式

    @Override
    public int doStartTag() throws JspException {

        long l = 0l;
        if (!StringUtils.isEmpty(value)) {
            l = Long.parseLong(value);
        }

        if (StringUtils.isEmpty(pattern)) {
            pattern = DEFAULT_FORMAT;
        }

        String targetTime = " ";

        if (l > 0L) {
            targetTime = new SimpleDateFormat(pattern).format(new Date(l));
        }

        try {
            super.pageContext.getOut().write(targetTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.doStartTag();
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

}